//
//  RequestManager.m
//  Leisure
//
//  Created by 沈鑫 on 16/3/11.
//  Copyright © 2016年 沈鑫. All rights reserved.
//

#import "RequestManager.h"

@implementation RequestManager

+ (void)requestWithUrl:(NSString *)urlString requestType:(RequestType )requestType parDic:(NSDictionary *)parDic finish:(Finish)finish error:(Error)error
{
    RequestManager *request = [[RequestManager alloc]init];
    [request requestWithUrl:urlString requestType:requestType parDic:parDic finish:finish error:error];
}

- (void)requestWithUrl:(NSString *)urlString requestType:(RequestType )requestType parDic:(NSDictionary *)parDic finish:(Finish)finish error:(Error)error
{
    // 保存属性
    self.finish = finish;
    self.error = error;
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    
    if (requestType == RequestTypePOST)
    {
        [request setHTTPMethod:@"POST"];
        if (parDic.count != 0)
        {
            [request setHTTPBody:[self dicToDataWithDic:parDic]];
        }
    }
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error)
        {
            self.error(error);
        }
        else
        {
            self.finish(data);
        }
    }];
    [task resume];
}

- (NSData *)dicToDataWithDic:(NSDictionary *)dic
{
    // 把字典里的键值对按照 key＝value 拼接成字符串，最后用&符号连接所有拼接好的字符串
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (NSString *key in dic)
    {
        NSString *string = [NSString stringWithFormat:@"%@=%@", key, dic[key]];
        [array addObject:string];
    }
    // 把数组里面的东西拼接成字符串
    NSString *dataString = [array componentsJoinedByString:@"&"];
    
    return [dataString dataUsingEncoding:NSUTF8StringEncoding];
}

@end
